package com.czz.manage.controller.teacher;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Course;
import com.czz.manage.query.TeacherCourseQuery;
import com.czz.manage.service.CourseService;
import com.czz.manage.vo.CourseInfoVo;
import com.czz.manage.vo.CoursePublishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: CourseTeacherController
 * @Author Czz
 * @Date 2020/1/19
 * @Time 20:27
 **/
@RestController
@RequestMapping(value = "/teacher/courses")
public class CourseTeacherController {

    @Autowired
    CourseService courseService;

    /**
    * @Description 保存课程信息
    * @Param [courseInfoVo]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/19
    * @Time 20:53
    **/
    @PostMapping(value = "")
    public R saveCourseInfo(CourseInfoVo courseInfoVo){
        if (ObjectUtils.isEmpty(courseInfoVo)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        String courseId = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }

    /**
    * @Description 根据课程id获取课程和课程描述信息
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/21
    * @Time 17:30
    **/
    @GetMapping(value = "/{id}")
    public R getCourseInfoById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        CourseInfoVo courseInfoVo = courseService.getCourseInfoById(id);
        return R.ok().data("item",courseInfoVo);
    }

    /**
    * @Description 根据课程id修改课程和课程描述信息
    * @Param [courseInfoVo, id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/22
    * @Time 9:47
    **/
    @PutMapping(value = "/{id}")
    public R updateCourseInfoById(CourseInfoVo courseInfoVo, @PathVariable String id){
        if (ObjectUtils.isEmpty(courseInfoVo) || StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        courseService.updateCourseInfoById(courseInfoVo);
        return R.ok();
    }

    /**
    * @Description 根据讲师id分页查询课程
    * @Param [page, limit, teacherCourseQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/22
    * @Time 15:37
    **/
    @GetMapping(value = "/{teacherId}/{page}/{limit}")
    public R getCoursePageList(@PathVariable String teacherId, @PathVariable Long page, @PathVariable Long limit, TeacherCourseQuery teacherCourseQuery){
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(teacherId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<Course> pageParam = new Page<>(page, limit);
        courseService.getTeacherCoursePageList(pageParam, teacherId, teacherCourseQuery);
        List<Course> records = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
    * @Description 根据id删除课程
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/22
    * @Time 15:49
    **/
    @DeleteMapping(value = "{id}")
    public R deleteCourseById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        courseService.deleteCourseById(id);
        return R.ok();
    }

    /**
    * @Description 根据课程id获取课程发布信息
    * @Param [courseId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/28
    * @Time 17:14
    **/
    @GetMapping(value = "/publish/{courseId}")
    public R getCoursePublishById(@PathVariable String courseId){
        if (StringUtils.isEmpty(courseId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        CoursePublishVo coursePublishVo = courseService.getCoursePublishById(courseId);
        return R.ok().data("item", coursePublishVo);
    }

    /**
    * @Description 根据id修改课程状态（上下架）
    * @Param [courseId, status]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/28
    * @Time 17:23
    **/
    @PutMapping(value = "/publish/{courseId}")
    public R publishCourseById(@PathVariable String courseId, Integer status){
        if (StringUtils.isEmpty(courseId) || StringUtils.isEmpty(status)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        courseService.publishCourseById(courseId, status);
        return R.ok();
    }
}
